/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.bezrabotnih.cabinet.client.ui;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.bezrabotnih.client.managed.request.JobProxy;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.dom.client.Style.VerticalAlign;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.layout.client.Layout.Alignment;
import com.google.gwt.place.shared.Place;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

/**
 * Sample implementation of {@link ProviderJobsView}.
 */
public class ProviderJobsViewImpl extends Composite implements ProviderJobsView {

	interface Binder extends UiBinder<Widget, ProviderJobsViewImpl> {
	}
	
	private static final Binder binder = GWT.create(Binder.class);
	
	private ProvidesKey<JobProxy> keyProvider=new ProvidesKey<JobProxy>() {

		@Override
		public Object getKey(JobProxy jobProxy) {
			return jobProxy.getId();
		}
	};
	
	private ProviderJobBlockView providerJobBlockView;

	@SuppressWarnings("unchecked")
	@UiField(provided=true) CellList<JobProxy> jobsList = new CellList<JobProxy>(new AbstractCell<JobProxy>() {

		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context,
				JobProxy value, SafeHtmlBuilder sb) {
			if (value == null) {
		        return;
		      }
			
			  sb.appendHtmlConstant("<div  style='border: 1px solid   grey; margin-top:3px'>");

		      sb.appendHtmlConstant("<table>");
		    
		      sb.appendHtmlConstant("<tr><td>");
		      
		      sb.appendHtmlConstant("<b>"+value.getName()+"</b>");
		      
		      sb.appendHtmlConstant("</td></tr><tr><td>");
		      
              sb.appendHtmlConstant("<b>Тип работы:</b>"+value.getJobType().getName());
		      
		      sb.appendHtmlConstant("</td></tr><tr><td>");
		    
		      sb.appendHtmlConstant("<b>Когда: </b>"+DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT).format(value.getStartDate()));
		      
		      sb.appendHtmlConstant("</td></tr><tr><td>");
		      
		      sb.appendHtmlConstant("<b>Стоимость: </b>"+value.getPrice()+" руб. "+(value.getIsHourly()?"в час":""));
		      
		      sb.appendHtmlConstant("</td></tr></table>");
		      
		      sb.appendHtmlConstant("</div>");
			
			
			
		}

		
	});
	@UiField SimplePanel jobPanel;
	
	
	
	private HashMap<String, ProviderJobBlockView> blockViews;
	
	private Presenter listener;
	
	private VerticalPanel jobsStackPanel;

	public ProviderJobsViewImpl() {
		initWidget(binder.createAndBindUi(this));
		blockViews=new HashMap<String, ProviderJobBlockView>();
		initJobsList();
		
	}
	
	
	private void initJobsList(){
	
		
		final SingleSelectionModel<JobProxy> selectionModel=new SingleSelectionModel<JobProxy>(keyProvider);
		
		
		jobsList.setSelectionModel(selectionModel);
		
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
			
			@Override
			public void onSelectionChange(SelectionChangeEvent arg0) {
			JobProxy jobProxy=selectionModel.getSelectedObject();
				listener.showJobDetails(jobProxy);
			}
		});
		
		
		
		
		
	}

	@Override
	public void setPresenter(Presenter listener) {
		this.listener = listener;
	}

	@Override
	public void addJobBlock(net.bezrabotnih.cabinet.client.ui.ProviderJobBlockView.Presenter listener, String jobId, String jobName,
			String jobType, String jobStatus, Boolean isHourly,
			Date startDate, Date deadLine, Integer price, String description) {
		
		ProviderJobBlockView blockView=new ProviderJobBlockView(listener, jobId, jobName, jobType, jobStatus, isHourly, startDate, deadLine, price, description);
		
		jobsStackPanel.add(blockView);
    	blockViews.put(jobId, blockView);
		
		
	}
	
	
	

	@Override
	public void setWinner(String jobId,String photoUrl, String name) {
		if (providerJobBlockView!=null){
			providerJobBlockView.setWinner(jobId,photoUrl,name);
		}
		
	}

	@Override
	public void clearApplicantsListPanel() {
		if (providerJobBlockView!=null){
			providerJobBlockView.clearApplicationListPanel();
		}
		
	}

	@Override
	public void addAddApplicant(String jobId,String applicantId,String photoUrl, String name) {
		if (providerJobBlockView!=null){
			providerJobBlockView.addApplicant(jobId,applicantId,photoUrl,name);
			
		}
		
	}

	@Override
	public void showApplicantsView() {
		if (providerJobBlockView!=null){
			providerJobBlockView.showApplicantsView();
		}
		
	}

	@Override
	public void clearJobsView() {
		if (jobsStackPanel!=null) jobsStackPanel.clear();
		if (blockViews!=null&&blockViews.size()>0) blockViews.clear();
		
	}
	
	
	public void setJobsTableData(List<JobProxy> jobProxies){
		jobsList.setRowData(jobProxies);
		if (jobProxies.size()>0) jobsList.getSelectionModel().setSelected(jobProxies.get(0),true);
	}


	@Override
	public SimplePanel getJobPanel() {
		return jobPanel;
	}


	@Override
	public void setJobDetailsView(ProviderJobBlockView providerJobBlockView) {
	jobPanel.setWidget(providerJobBlockView);
	this.providerJobBlockView=providerJobBlockView;
		
	}


	@Override
	public void addComment(String thumbnailUrl, String name, String text) {
		this.providerJobBlockView.addComment(thumbnailUrl, name, text);
		
	}
	



}